package com.ruoyi.fyhs.controller.lld;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.fyhs.domain.lld.KtLldFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.fyhs.domain.lld.KtLld;
import com.ruoyi.fyhs.service.lld.IKtLldService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 费用核算领料单Controller
 *
 * @author huacai
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/fyhs/lld")
public class KtLldController extends BaseController
{
    @Autowired
    private IKtLldService ktLldService;

    /**
     * 查询费用核算领料单列表
     */
    @PreAuthorize("@ss.hasPermi('fyhs:lld:list')")
    @GetMapping("/list")
    public TableDataInfo list(KtLld ktLld)
    {
        startPage();
        List<KtLld> list = ktLldService.selectKtLldList(ktLld);
        return getDataTable(list);
    }

    /**
     * 导出费用核算领料单列表
     */
    @PreAuthorize("@ss.hasPermi('fyhs:lld:export')")
    @Log(title = "费用核算领料单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KtLld ktLld)
    {
        List<KtLld> list = ktLldService.selectKtLldList(ktLld);
        ExcelUtil<KtLld> util = new ExcelUtil<KtLld>(KtLld.class);
        util.exportExcel(response, list, "费用核算领料单数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<KtLld> util = new ExcelUtil<KtLld>(KtLld.class);
        util.importTemplateExcel(response, "费用核算领料单数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "费用核算领料单", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('fyhs:lld:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<KtLld> util = new ExcelUtil<KtLld>(KtLld.class);
        InputStream inputStream = file.getInputStream();
        List<KtLld> list = util.importExcel(inputStream );
        inputStream.close();
        int count = ktLldService.batchInsertKtLld(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取费用核算领料单详细信息
     */
    @PreAuthorize("@ss.hasPermi('fyhs:lld:query')")
    @GetMapping(value = "/{lldId}")
    public AjaxResult getInfo(@PathVariable("lldId") Integer lldId)
    {
        return success(ktLldService.selectKtLldByLldId(lldId));
    }

    /**
     * 新增费用核算领料单
     */
    @PreAuthorize("@ss.hasPermi('fyhs:lld:add')")
    @Log(title = "费用核算领料单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KtLld ktLld)
    {
        return toAjax(ktLldService.insertKtLld(ktLld));
    }

    /**
     * 修改费用核算领料单
     */
    @PreAuthorize("@ss.hasPermi('fyhs:lld:edit')")
    @Log(title = "费用核算领料单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KtLld ktLld)
    {
        return toAjax(ktLldService.updateKtLld(ktLld));
    }

    /**
     * 删除费用核算领料单
     */
    @PreAuthorize("@ss.hasPermi('fyhs:lld:remove')")
    @Log(title = "费用核算领料单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lldIds}")
    public AjaxResult remove(@PathVariable Integer[] lldIds)
    {
        return toAjax(ktLldService.deleteKtLldByLldIds(lldIds));
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertKtLldFjByxmsbId(@RequestBody KtLldFj ktLldFj)
    {
        return toAjax(ktLldService.insertKtLldFjByxmsbId(ktLldFj));
    }

    /**
     * 根据主表ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectKtLldFjList(Integer sId)
    {
        KtLldFj ktLldFj = new KtLldFj();
        ktLldFj.setLldId(sId);
        return AjaxResult.success(ktLldService.selectKtLldFjList(ktLldFj));
    }

    /**
     * 获取领料单明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:query')")
    @GetMapping(value = "/lldmx/{lldId}")
    public AjaxResult selectKtLldMxByLldId(@PathVariable("lldId") Integer lldId) {
        return success(ktLldService.selectKtLldMxByLldId(lldId));
    }

}
